package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.RestBean;
import com.hs.entity.dto.Team;
import com.hs.entity.dto.UpdatePassword;
import com.hs.entity.dto.User;
import com.hs.service.UserService;
import io.lettuce.core.dynamic.annotation.Param;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/getUserList/{currentNum}")
    public RestBean<PageInfo<User>> getUserList(
            @PathVariable("currentNum") Integer currentNum,
            @RequestParam(required = false) String keywords) {
        PageInfo<User> userList = userService.selectUserList(currentNum,keywords);
        return RestBean.success(userList);
    }

    @GetMapping("/getUserById/{id}")
    public RestBean<User> getUserById(@PathVariable Integer id) {
//        System.out.println(id);

        User user = userService.getById(id);
        return RestBean.success(user);
    }

    @PostMapping("/updateUser")
    public RestBean<User> updateUser( @RequestBody User user) {
        System.out.println(user);
        userService.updateById(user);
        return RestBean.success(null);
    }

    @PostMapping("/addUser")
    public RestBean<User> addUser( @RequestBody User user) {
        user.setCreate_time(LocalDateTime.now());
        user.setRole(1);
        userService.save(user);
        return RestBean.success(null);
    }

    @PostMapping("/deleteUser/{id}")
    public RestBean<User> deleteUser(@PathVariable  Integer id) {
        userService.removeById(id);
        return RestBean.success(null);
    }


    @PostMapping("/userByUsername/{username}")
    public RestBean<User> userByUsername(@PathVariable  String  username) {
        User loginUser = userService.userByUsername(username);
        return RestBean.success(loginUser);
    }

    @PostMapping("/updatePassword")
    public RestBean<String> updatePassword(@RequestBody UpdatePassword dto) {
        boolean result = userService.updatePassword(dto);
        return result ? RestBean.success("密码修改成功") : RestBean.failure(401, "原密码错误");
    }

}
